package com.example.controller;

import com.example.service.GroupService;
import com.example.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/group")
public class GroupController {
    // 注入 GroupService 接口
    @Autowired
    private GroupService groupService;
    @PostMapping("/list")
    public Object list(@RequestBody Map<String, Object> paramMap) {
        // 这里可以对传入的 paramMap 进行一些验证和处理
        // 调用 getStudentScores 方法并传递参数
        return AjaxResult.success(groupService.getStudentScores(paramMap));
    }

    // 获取小组排名
    @PostMapping("/ranking")
    public Object getGroupRanking(@RequestBody Map<String, Object> paramMap) {
        try {
            Map<String, List<Object>> ranking = groupService.getGroupRanking(paramMap);
            return AjaxResult.success(ranking);
        } catch (Exception e) {
            return AjaxResult.fail("获取小组排名失败: " + e.getMessage());
        }
    }

    // 学生前十的信息
    @PostMapping("/topTen")
    public Object getTopTenStudentScores(@RequestBody Map<String, Object> paramMap) {
        try {
            Map<String, Object> topTenStudents = groupService.getTopTenStudentScores(paramMap);
            return AjaxResult.success(topTenStudents);
        } catch (Exception e) {
            return AjaxResult.fail("获取成绩排名前十的学生信息失败: " + e.getMessage());
        }
    }

    // 获取任务统计信息
    @PostMapping("/missionStats")
    public Object getMissionStats(@RequestBody Map<String, Object> params) {
        try {
            Map<String, List<Object>> missionStats = groupService.getMissionStats(params);
            return AjaxResult.success(missionStats);
        } catch (Exception e) {
            return AjaxResult.fail("获取任务统计信息失败: " + e.getMessage());
        }
    }

    // 统计不同组号的平均成绩
    @PostMapping("/averageScoreByGroup")
    public Object getAverageScoreByGroup(@RequestBody Map<String, Object> params) {
        try {
            Map<String, List<Object>> missionStats = groupService.getAverageScoreByGroup(params);
            return AjaxResult.success(missionStats);
        } catch (Exception e) {
            return AjaxResult.fail("获取小组任务统计信息失败: " + e.getMessage());
        }
    }

    // 获取所有互动记录
    @GetMapping("/allInteractRecords")
    public Object getAllInteractRecords() {
        try {
            List<Map<String, Object>> interactRecords = groupService.getAllInteractRecords();
            return AjaxResult.success(interactRecords);
        } catch (Exception e) {
            return AjaxResult.fail("获取所有互动记录失败: " + e.getMessage());
        }
    }

    // 获取小组统计信息
    @PostMapping("/missionStatsGroup")
    public Object getMissionStatsByGroup(@RequestBody Map<String, Object> params) {
        try {
            Map<String, List<Object>> missionStats = groupService.getMissionStatsByGroup(params);
            return AjaxResult.success(missionStats);
        } catch (Exception e) {
            return AjaxResult.fail("获取任务统计信息失败: " + e.getMessage());
        }
    }
    // 获取预警数据的方法
    @PostMapping("/warningData")
    public Object getWarningData(@RequestBody Map<String, Object> params) {
        try {
            List<Map<String, Object>> warningData = groupService.getWarningData(params);
            return AjaxResult.success(warningData);
        } catch (Exception e) {
            return AjaxResult.fail("获取预警数据失败: " + e.getMessage());
        }
    }
    // 查询课中错误数据
    @PostMapping("/error")
    public Object getDuringErrorData(@RequestBody Map<String, Object> params) {
        try {
            List<Map<String, Object>> warningData = groupService.getDuringErrorData(params);
            return AjaxResult.success(warningData);
        } catch (Exception e) {
            return AjaxResult.fail("查询课中错误数据: " + e.getMessage());
        }
    }
    //         定义课中平均score的 CTE，计算所有学生的总分
    @PostMapping("/stats")
    public Object getGroupStats(@RequestBody Map<String, Object> params) {
        try {
            Map<String, Object> stats = groupService.yourOriginalSqlId(params);
            return AjaxResult.success(stats);
        } catch (Exception e) {
            return AjaxResult.fail("获取小组统计信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据班级号和任务类型获取错误数据
     * @param classId
     * @param missionType
     * @return
     */
    @GetMapping("/fault/{classId}/{missionType}")
    public Object getFaultByClassIdAndMissionType(@PathVariable Integer classId, @PathVariable String missionType){
        return AjaxResult.success(groupService.getFaultByClassIdAndMissionType(classId,missionType));
    }
}